package com.ciban.smarthome.module.engine;

import android.content.Context;

import com.ciban.smarthome.common.utils.StringUtils;
import com.ciban.smarthome.common.utils.logcrash.LogUtil;
import com.ciban.smarthome.module.Constants;
import com.ciban.smarthome.module.db.dao.AreaDao;
import com.ciban.smarthome.module.db.dao.EquipmentDao;
import com.ciban.smarthome.module.db.dao.HostDao;
import com.ciban.smarthome.module.db.dao.LampDao;
import com.ciban.smarthome.module.db.dao.SceneDao;
import com.ciban.smarthome.module.engine.base.BaseEngine;
import com.ciban.smarthome.module.entity.Area;
import com.ciban.smarthome.module.entity.Equip_Lamp;
import com.ciban.smarthome.module.entity.Equipment;
import com.ciban.smarthome.module.entity.Host;
import com.ciban.smarthome.module.entity.Scene;
import com.ciban.smarthome.module.net.communication.Communicate;
import com.ciban.smarthome.module.net.communication.ProtocolHelper;
import com.ciban.smarthome.module.utils.ModuleUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by wdl on 2015/11/9.
 * 对于情景模式相关的管理类
 */
public class SceneEngine extends BaseEngine {
    private SceneDao dao;
    private ArrayList<Scene> sceneList;


    public SceneEngine(Context context) {
        super(context);
    }

    /*自定义场景*/
    private ArrayList<Area> areaList;
    private ArrayList<ArrayList<Equipment>> equiList;
    private String scene_id;  //预生成的场景id
    private HashSet<String> equips;//该情景对应的设备id列表

    public ArrayList<Area> getAreaList() {
        if (areaList == null)
            areaList = getAreaListFromDb();
        return areaList;
    }

    public ArrayList<ArrayList<Equipment>> getEquiList() {
        if (equiList == null)
            equiList = getEquipmentsByAreaList();
        return equiList;
    }

    /**
     * 添加场景对应的设备
     * @param equipID
     */
    public void addSceneEquip(String equipID){
        if (equips==null)
            equips=new HashSet<String>();
        equips.add(equipID);
    }

    /**
     * 生成情景模式的ID
     * @return
     */
    public String getScene_id() {
        if (scene_id == null)
            scene_id = ModuleUtils.getIDByRule();
        return scene_id;
    }

    /**
     * 根据情景id 设备id 设备类型
     * 获取该类型的配置信息
     *
     * @param sceneID
     * @param equipID
     * @param type
     * @param <T>
     * @return
     */
    public <T> T getEquipByType(String sceneID, String equipID, int type) {
        T config = null;
        switch (type) {
            case Constants.EQUIP_AIR_TYPE:

                break;
            case Constants.EQUIP_LAMP_TYPE:
                LampDao dao = new LampDao(context);
                config = (T) dao.getLampConfig(equipID, sceneID);
                dao = null;
                break;
            case Constants.EQUIP_CURTAIN_TYPE:

                break;
            case Constants.EQUIP_TV_TYPE:

                break;
            case Constants.EQUIP_PROJECTOR_TYPE:

                break;
        }
        return config;
    }


    /**
     * 取消场景自定义
     * 预处理的时候是将设备操作给写到了各自的配置数据库中，
     * 在这里需要将写的数据进行清空，在这里暂时预留 后续开线程清空
     */
    public void diySceneCancle() {

    }

    /**
     * 单个设备的状态已经保存，
     * 这里只需要将场景信息进行保存
     */
    public boolean saveScene() {
        String result="";
        for (String info:equips){
            result+=info+",";
        }

        Scene scene=new Scene();
        scene.setName("测试");
        scene.setEquip_list(result);
        checkDao();
        long l = dao.addNewScene(scene);

        LogUtil.info(result);

        return l>0?true:false;

    }

    /**
     * 发送命令
     * @param info
     */
    public void sendData(Scene info,Communicate.ConnListener listener){
        if (!StringUtils.isEmpty(info.getSerial_no()+"")&&!StringUtils.isEmpty(info.getHost_serno())){
            HostDao hdao=new HostDao(context);
            Host host = hdao.getMainEngineDetailbySerno(info.getHost_serno());
            String cmd = ProtocolHelper.getSceneCmd(host,
                    info.getSerial_no());

            Communicate comm=new Communicate();
            comm.sendData(host, cmd, listener);
        }

    }

    /**
     * 获取所有区域对应的所有设备
     *
     * @return
     */
    private ArrayList<ArrayList<Equipment>> getEquipmentsByAreaList() {
        ArrayList<ArrayList<Equipment>> list = new ArrayList<ArrayList<Equipment>>();
        EquipmentDao edao = new EquipmentDao(context);
        ArrayList<Equipment> equipListByArea;
        for (Area info : areaList) {
            equipListByArea = edao.getEquipListByArea(info.getId());
            list.add(equipListByArea);
        }
        edao = null;
        return list;
    }

    public ArrayList<Equipment> getEquiByArea(String area_id) {
        EquipmentDao edao = new EquipmentDao(context);
        ArrayList<Equipment> equipListByArea = edao.getEquipListByArea(area_id);
        edao = null;
        return equipListByArea;
    }

    /**
     * 获取区域列表
     */
    private ArrayList<Area> getAreaListFromDb() {
        AreaDao adao = new AreaDao(context);
        ArrayList<Area> areaList = adao.getAreaList();
        adao = null;
        return areaList;
    }


    /*场景相关*/

    /**
     * 获取场景的列表
     *
     * @return
     */
    public ArrayList<Scene> getSceneList() {
        if (sceneList == null) {
            checkDao();
            sceneList = dao.getSceneList();
        }
        return sceneList;
    }

    /**
     * 当数据更新以后 刷新场景列表
     */
    public void refreshSceneList() {
        checkDao();
        sceneList.clear();
        sceneList.addAll(dao.getSceneList());
    }

    /**
     * 添加新的情景模式
     *
     * @param info
     * @return
     */
    public boolean addNewScene(Scene info) {
        SceneDao dao = new SceneDao(context);
        return dao.addNewScene(info) > 0;
    }

    /**
     * 删除情景模式
     *
     * @param id
     * @return
     */
    public boolean delScene(String id) {
        checkDao();
        return dao.delSceneById(id) > 0;
    }

    public void destory() {
        dao = null;
    }

    private void checkDao() {
        if (dao == null)
            dao = new SceneDao(context);
    }

}
